var dataManageMain;
var dataManageGrid;
var dataManageGrid_cm;
var dataManageGrid_Store;
var dataManageGrid_StoreData=new Object();

var newForm;
var newWin;
var dayCostSearchForm;
var editos=0;//判断修改2还是添加1
var editos_dcID;//修改时用于存储gird选中的ID号
var downDate;// 由于输入日期air会报异常，为了禁止输入所加变量(有bug,找到其他方法再替换)

//查询所有数据
function selectDayCostData(){
	
	dataManageGrid_StoreData=dayCostDao.select();
	
	dataManageGrid_Store.loadData(dataManageGrid_StoreData);
}

// 查询指定年，月，
function searchMonthCostData(yearData, monthData) {

	var sql = "where dc_year='" + yearData + "' and dc_month='" + monthData
			+ "' order by dc_day desc";
			
	dataManageGrid_StoreData = dayCostDao.select(sql);
	
	dataManageGrid_Store.loadData(dataManageGrid_StoreData);
}

//修改数据
function updateDayCostData(entity,pryID){
	dayCostDao.updateBy(entity,'dcID='+pryID);
}
//添加数据
function addDayCostData(entity){
	dayCostDao.insert(entity);
}
//删除数据
function deleteDayCostData(pryID){
	dayCostDao.removeBy('dcID='+pryID);
}

var dayCostSearchForm_downDate;
var dayCostSearchForm_date = new Ext.form.DateField({
	fieldLabel : '日期',
	name : 'dayCostSearchForm_date',
	format : 'Y-m',
	value : new Date(),
	width : 125,
	enableKeyEvents : true,
	listeners : {
		'keydown' : function() {
			dayCostSearchForm_downDate = dayCostSearchForm_date.getValue();
		},
		'keyup' : function() {
			dayCostSearchForm_date.setValue(dayCostSearchForm_downDate);
		}
	}
});

var dayCostSearchForm_select=new Ext.Button({
	text: '查询',
	handler: function(){
		
		if(dayCostSearchForm_date.getValue()==''){
			selectDayCostData();
		}
		else{
			searchMonthCostData(dayCostSearchForm_date.getValue().getFullYear(),dayCostSearchForm_date.getValue().getMonth()+1);
		}
	}
});

dayCostSearchForm = new Ext.form.FormPanel({
	width:692,
	height:70,
	title:'数据查询',
    frame:true,
    region:'center',
    labelAlign:'right',
    layout:'column',
    items: [
        {columnWidth: .6, layout: 'form',style:'margin-left:150px',
        	items: [dayCostSearchForm_date]
        },
        {columnWidth: .4, layout: 'form', 
        	items: [dayCostSearchForm_select]
        }
    ]
});

var newForm_date = new Ext.form.DateField({
	fieldLabel : '日期',
	name : 'newForm_date',
	format : 'Y-m-d',
	value : new Date(),
	width : 125,
	allowBlank:false,
	enableKeyEvents : true,
	listeners : {
//		'focus' : function() {
//			this.onTriggerClick();// 调用日期下拉
//		},
		'keydown' : function() {
			downDate = newForm_date.getValue();
		},
		'keyup' : function() {
			newForm_date.setValue(downDate);
		}
	}
});

var newForm_cost = new Ext.form.TextField({
	fieldLabel : '消费￥',
	name : 'newForm_cost',
	allowBlank:false,
	width : 125
});

newForm = new Ext.form.FormPanel({
	baseCls : 'x-plain',
	labelAlign : 'right',
	items : [newForm_date, newForm_cost]
});

newWin = new Ext.Window({
	title : 'day cost',
	resizable : false,
	width : 320,
	height : 128,
	layout : 'fit',
	bodyStyle : 'padding:5px;',
	closeAction : 'hide',
	plain : true,
	items : [newForm],
	buttons : [{
		text : 'OK',
		handler : function() {
			if(isNaN(newForm_cost.getValue())||newForm_cost.getValue()==''){
				Ext.MessageBox.alert('输入错误','请输入数字!');
				return ;
			}
			//添加
			if(editos==1){
//				var id = Number(dataManageGrid_StoreData[dataManageGrid_StoreData.length-1].dcID)+1;
				var entity={
					dc_year:newForm_date.getValue().getFullYear(),
					dc_month:newForm_date.getValue().getMonth()+1,
					dc_day:newForm_date.getValue().getDate(),
					dc_cost:newForm_cost.getValue()
				 };
	 			addDayCostData(entity);
			}//修改
			else if(editos==2){
				var entity={
//					dcID:editos_dcID,
					dc_year:newForm_date.getValue().getFullYear(),
					dc_month:newForm_date.getValue().getMonth()+1,
					dc_day:newForm_date.getValue().getDate(),
					dc_cost:newForm_cost.getValue()
				 };
	 			updateDayCostData(entity,editos_dcID)
			}
			
 			selectDayCostData();
 			newWin.hide();
		}
	}, {
		text : 'Cancel',
			handler : function() {
				newWin.hide();
			}
	}]
});

dataManageGrid_Store=new Ext.data.JsonStore({
	fields:['dcID','dc_year','dc_month','dc_day','dc_cost']
//	data:dataManageGrid_StoreData	
});
//dataManageGrid_Store=new Ext.data.SimpleStore({
//	fields:dayCost,
//	id:'dataManageGrid_StoreId'
////	data:dataManageGrid_StoreData	
//});

dataManageGrid_cm=new Ext.grid.ColumnModel([
	{id:'dcID',header:'ID',width:100,align:'center',dataIndex:'dcID'},
	{header:'年',width:100,align:'center',dataIndex:'dc_year'},
	{header:'月',width:100,align:'center',dataIndex:'dc_month'},
	{header:'日',width:100,align:'center',dataIndex:'dc_day'},
	{header:'消费',width:266,align:'center',dataIndex:'dc_cost'}
]);

dataManageGrid=new Ext.grid.GridPanel({
//	title:'日期数据列表',
	width:692,
	height:290,
	region:'south',
	loadMask:true,
	stripeRows:true,
	sm:new Ext.grid.RowSelectionModel({singleSelect:true}),
	cm:dataManageGrid_cm,
	store:dataManageGrid_Store,
	tbar:new Ext.Toolbar({
		buttons : [{
			text : "日常消费录入",
			handler : function() {
				editos=1;
				newWin.show(this);
			}
		}, {
			text : "日常消费修改",
			handler : function() {
				editos=2;
				var dg=dataManageGrid.getSelections();
				if(dg==''){
					Ext.MessageBox.alert('无法修改','请选择一条数据!');
					return ;
				}
				newWin.show(this);
				editos_dcID=dg[0].data.dcID;
				newForm_date.setValue(new Date(dg[0].data.dc_year,dg[0].data.dc_month-1,dg[0].data.dc_day));
				newForm_cost.setValue(dg[0].data.dc_cost);
			}
		},{
			text : "日常数据删除",
			handler : function() {
				
				Ext.Msg.show({
					title : '删除日期?(建议不要删除日期)',
					msg : '确定要删除这条数据?',
					buttons : Ext.Msg.YESNO,
					fn : function(btn, text) {
						if (btn == "yes") {
							var dg=dataManageGrid.getSelections();
							if(dg==''){
								Ext.MessageBox.alert('无法删除','请选择一条数据!');
								return ;
							}
							deleteDayCostData(dg[0].data.dcID);
							selectDayCostData();
						}
					},
					animEl : 'elId',
					icon : Ext.MessageBox.QUESTION
				});
				
			}
		}
	  ]
	})
//    bbar: new Ext.PagingToolbar({
//    	pageSize :10,
//    	displayInfo :true,
//    	store: dataManageGrid_Store
//    })
});

dataManageMain=new Ext.Panel({
	width:692,
	height:350,
	layout:'border',
	items:[dayCostSearchForm,dataManageGrid]
});

Ext.onReady(function(){

	selectDayCostData();
	
	dataManageMain.render('dataManageMain');
})